package controllers;

import db.DriverDAO;
import db.UserDAO;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class User {

    private int id;
    private String username;
    private String password;
    private int permissionLevelID;
    private int driverID;
    private String permissionLevelDesc;
    private String driverName;
    private static User editableUser;
    private static User loggedInUser;

    //<editor-fold desc="Constructors">
    public User() {
    }

    public User(int id, String username, String password, int permissionLevelID, int driverID,
            String permissionLevelDesc, String driverName) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.permissionLevelID = permissionLevelID;
        this.driverID = driverID;
        this.permissionLevelDesc = permissionLevelDesc;
        this.driverName = driverName;
    }
    //</editor-fold>

    //<editor-fold desc="Getters and Setters">
    public int getId() {
        return id;
    }

    public int getDriverID() {
        return driverID;
    }

    public void setDriverID(int driverID) {
        this.driverID = driverID;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPermissionLevelID() {
        return permissionLevelID;
    }

    public void setPermissionLevelID(int permissionLevelID) {
        this.permissionLevelID = permissionLevelID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public static User getLoggedInUser() {
        return loggedInUser;
    }

    public static void setLoggedInUser(User loggedInUser) {
        User.loggedInUser = loggedInUser;
    }

    public String getPermissionLevelDesc() {
        return permissionLevelDesc;
    }

    public void setPermissionLevelDesc(String permissionLevelDesc) {
        this.permissionLevelDesc = permissionLevelDesc;
    }
    //</editor-fold>

    //<editor-fold desc="Editable Driver">
    public void setEditableUser(User user) {
        editableUser = user;
    }

    public User getEditableUser() {
        return editableUser;
    }

    public String editDriver() {
        FacesContext fc = FacesContext.getCurrentInstance();
        setEditableUser((User) fc.getExternalContext().getRequestMap().get("user"));
        return "editUser";
    }
    //</editor-fold>

    //<editor-fold desc="CRUD Methods">
    public String addUser() {
        if (UserDAO.addUser(this)) {
            return "userList";
        } else {
            return "userAddFailure";
        }
    }

    public String deleteUser() {
        if (UserDAO.deleteUser(this)) {
            return "userList";
        } else {
            return "userDeleteFailure";
        }
    }

    public List<User> getUsers() {
        return UserDAO.getUsers();
    }

    public String saveUser() {
        if (UserDAO.saveUser(this)) {
            editableUser = null;
            return "userList";
        } else {
            return "userSaveFailure";
        }
    }
    //</editor-fold>

    public String login() {

        User checkUser = UserDAO.getUserByUserName(this.getUsername());
        int permission = checkUser.getPermissionLevelID();
        if (checkUser.getPassword().equals(this.getPassword())) {
            loggedInUser = checkUser;
            Driver.setLoggedDriverStatic(DriverDAO.getDriverByID(checkUser.getDriverID()));
            if (permission == 1) {
                return "adminHome";
            } else if (checkUser.getDriverID() == 0) {
                return "addDriver";
            } else {
                return "home";
            }
        } else {
            return "login";
        }
    }

    public String returnHome() {
        if (this.permissionLevelID == 1) {
            return "adminHome";
        }
        return "home";
    }

    @Override
    public String toString() {
        return this.username;
    }
}
